Xbasic

Extension::JsonJsonToCSV Method

Syntax

.JsonToCSV as c (json as C [, columns as C [, tagName as C]])

Arguments

jsonCharacter

Json array to convert to CSV.

columnsCharacter

Optional columns (will export all columns to CSV in order they appear in json if ommitted).

tagNameCharacter

Optional name of property that contains array (if array is not the top level json).

Description

Convert JSON data to a CSV array, optional comma delimited 'columns' are used if supplied to select columns.

Example

dim json as c
json = <<%json%
[
{ "ticker" : "AAPL" , "name" : "Apple Inc." , "price" : "402.215" , "change" : "-24.025" , "mktcap" : "377.7B" , "chgPct" : "-24.025 - -5.64%"} ,
{ "ticker" : "GOOG" , "name" : "Google Inc." , "price" : "780.37" , "change" : "-13.00" , "mktcap" : "257.3B" , "chgPct" : "-13.00 - -1.64%"} ,
{ "ticker" : "MSFT" , "name" : "Microsoft Corpora" , "price" : "28.8349" , "change" : "-0.1351" , "mktcap" : "241.5B" , "chgPct" : "-0.1351 - -0.47%"} ,
{ "ticker" : "INTC" , "name" : "Intel Corporation" , "price" : "22.067" , "change" : "+0.152" , "mktcap" : "109.1B" , "chgPct" : "+0.152 - +0.69%"} ,
{ "ticker" : "HPQ" , "name" : "Hewlett-Packard C" , "price" : "20.48" , "change" : "-0.55" , "mktcap" : "39.814B" , "chgPct" : "-0.55 - -2.62%"} ,
{ "ticker" : "ORCL" , "name" : "Oracle Corporatio" , "price" : "32.528" , "change" : "-0.887" , "mktcap" : "153.2B" , "chgPct" : "-0.887 - -2.65%"} ,
{ "ticker" : "CSCO" , "name" : "Cisco Systems, In" , "price" : "20.625" , "change" : "-0.535" , "mktcap" : "110.0B" , "chgPct" : "-0.535 - -2.53%"} ,
{ "ticker" : "DELL" , "name" : "Dell Inc." , "price" : "14.005" , "change" : "-0.035" , "mktcap" : "24.470B" , "chgPct" : "-0.035 - -0.25%"} ,
{ "ticker" : "YHOO" , "name" : "Yahoo! Inc." , "price" : "23.696" , "change" : "-0.094" , "mktcap" : "26.096B" , "chgPct" : "-0.094 - -0.40%"} ,
{ "ticker" : "FB" , "name" : "Facebook, Inc." , "price" : "26.5912" , "change" : "-0.3288" , "mktcap" : "63.344B" , "chgPct" : "-0.3288 - -1.22%"} ,
{ "ticker" : "LNKD" , "name" : "LinkedIn Corporat" , "price" : "180.29" , "change" : "-2.18" , "mktcap" : "19.661B" , "chgPct" : "-2.18 - -1.19%"} ,
{ "ticker" : "GRPN" , "name" : "Groupon, Inc." , "price" : "6.37" , "change" : "-0.01" , "mktcap" : "4.194B" , "chgPct" : "-0.01 - -0.16%"} ,
{ "ticker" : "YELP" , "name" : "Yelp Inc. Class A" , "price" : "25.545" , "change" : "-0.165" , "mktcap" : "1.628B" , "chgPct" : "-0.165 - -0.64%"}
]
%json%

? extension::json::JsonToCSV(json,"ticker,name,price") 	
"ticker","name","price"
"AAPL","Apple Inc.","402.215"
"GOOG","Google Inc.","780.37"
"MSFT","Microsoft Corpora","28.8349"
"INTC","Intel Corporation","22.067"
"HPQ","Hewlett-Packard C","20.48"
"ORCL","Oracle Corporatio","32.528"
"CSCO","Cisco Systems, In","20.625"
"DELL","Dell Inc.","14.005"
"YHOO","Yahoo! Inc.","23.696"
"FB","Facebook, Inc.","26.5912"
"LNKD","LinkedIn Corporat","180.29"
"GRPN","Groupon, Inc.","6.37"
"YELP","Yelp Inc. Class A","25.545"